---
import CaptchaScripts from '../../components/Captcha/CaptchaScripts.astro';
import FAQs from '../../components/FAQs/FAQs.astro';
import FrameRenderer from '../../components/FrameRenderer/FrameRenderer.astro';
import MarkdownFile from '../../components/MarkdownFile.astro';
import RelatedRoadmaps from '../../components/RelatedRoadmaps.astro';
import RoadmapHeader from '../../components/RoadmapHeader.astro';
import ShareIcons from '../../components/ShareIcons/ShareIcons.astro';
import TopicOverlay from '../../components/TopicOverlay/TopicOverlay.astro';
import UpcomingForm from '../../components/UpcomingForm.astro';
import BaseLayout from '../../layouts/BaseLayout.astro';
import { generateArticleSchema, generateFAQSchema } from '../../lib/jsonld-schema';
import { getRoadmapIds, RoadmapFrontmatter } from '../../lib/roadmap';

export async function getStaticPaths() {
  const roadmapIds = await getRoadmapIds();

  return roadmapIds.map((roadmapId) => ({
    params: { roadmapId },
  }));
}

interface Params extends Record<string, string | undefined> {
  roadmapId: string;
}

const { roadmapId } = Astro.params as Params;
const roadmapFile = await import(`../../data/roadmaps/${roadmapId}/${roadmapId}.md`);
const { faqs: roadmapFAQs = [] } = await import(`../../data/roadmaps/${roadmapId}/faqs.astro`);
const roadmapData = roadmapFile.frontmatter as RoadmapFrontmatter;

let jsonLdSchema = [];

if (roadmapData.schema) {
  const roadmapSchema = roadmapData.schema;
  jsonLdSchema.push(
    generateArticleSchema({
      url: `https://roadmap.sh/${roadmapId}`,
      headline: roadmapSchema.headline,
      description: roadmapSchema.description,
      datePublished: roadmapSchema.datePublished,
      dateModified: roadmapSchema.dateModified,
      imageUrl: roadmapSchema.imageUrl,
    })
  );
}

if (roadmapFAQs.length) {
  jsonLdSchema.push(generateFAQSchema(roadmapFAQs));
}

const contentContributionLink = `https://github.com/kamranahmedse/developer-roadmap/tree/master/src/data/roadmaps/${roadmapId}/content`;
---

<BaseLayout
  permalink={`/${roadmapId}`}
  title={roadmapData?.seo?.title}
  description={roadmapData.seo.description}
  keywords={roadmapData.seo.keywords}
  sponsor={roadmapData.sponsor}
  noIndex={roadmapData.isUpcoming}
  jsonLd={jsonLdSchema}
>
  <!-- Preload the font being used in the renderer -->
  <link rel='preload' href='/fonts/balsamiq.woff2' as='font' type='font/woff2' crossorigin slot='after-header' />

  <RoadmapHeader
    title={roadmapData.title}
    description={roadmapData.description}
    note={roadmapData.note}
    tnsBannerLink={roadmapData.tnsBannerLink}
    roadmapId={roadmapId}
    hasTopics={roadmapData.hasTopics}
    isUpcoming={roadmapData.isUpcoming}
  />

  <div class='bg-gray-50 pt-4 sm:pt-12'>
    {
      !roadmapData.isUpcoming && roadmapData.jsonUrl && (
        <div class='max-w-[1000px] container relative'>
          <ShareIcons description={roadmapData.briefDescription} pageUrl={`https://roadmap.sh/${roadmapId}`} />
          <TopicOverlay contentContributionLink={contentContributionLink} />

          <FrameRenderer
            resourceType={'roadmap'}
            resourceId={roadmapId}
            jsonUrl={roadmapData.jsonUrl}
            dimensions={roadmapData.dimensions}
          />
        </div>
      )
    }

    {
      !roadmapData.isUpcoming && !roadmapData.jsonUrl && (
        <div class='mt-0 sm:-mt-6 pb-14'>
          <MarkdownFile>
            <roadmapFile.Content />
          </MarkdownFile>
        </div>
      )
    }

    {roadmapData.isUpcoming && <UpcomingForm />}

    <FAQs faqs={roadmapFAQs} />
    <RelatedRoadmaps roadmap={roadmapData} />
  </div>
</BaseLayout>
